/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: true

    },
    {
        id: 'user.nickName',
        title: '用户昵称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'user.mobile',
        title: '手机号码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'idcard',
        title: '身份证号',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'authStatus',
        title: '认证状态',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.realNameStatus.WAIT,
            2:$lang.realNameStatus.PASS,
            3:$lang.realNameStatus.NOPASS,
        }
    },
    {
        id: 'type',
        title: '认证方式',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.realNameType.interface,
            2:$lang.realNameType.manual
        }
    },
    {
        id: 'certificateTime',
        title: '审核时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var verifyHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;审核&nbsp;</a>";
            var look = "<a data-target='#lookModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-eye'></i>&nbsp;查看&nbsp;</a>";
            var html = "";
            // 判断是否有审核权限
            if ($("#realAudit") && $("#realAudit").val() == 1) {
                if (record.authStatus == 1 && record.type == 2) {
                    html += verifyHtml;
                }
            }
            // 判断是否有查看权限
            if ($("#realLook") && $("#realLook").val() == 1) {
                html += look;
            }
            return html;
        }
    },
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/userAuth/json/list',
    exportFileName: '实名认证列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","authStatus","userId","type"],Timestamp:["certificateTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
//排序
grid.sortParameter.columnId = ['asc_authStatus'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //审核对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#id").val(gridData.id);
            $("#nickName").val(gridData.user.nickName);
            $("#mobilePhone").val(gridData.user.mobile);
            $("#idcard").val(gridData.idcard);
            $("#realName").val(gridData.realName);
            var aStatus = gridData.authStatus;
            if( aStatus==1){
                $("#authStatus").val($lang.realNameStatus.WAIT);
            }else if(aStatus == 2 ){
                $("#authStatus").val($lang.realNameStatus.PASS);
            }else if (aStatus ==3 ){
                $("#authStatus").val($lang.realNameStatus.NOPASS);
            }
            var type = gridData.type;
            if(type == 1 ){
                $("#type").val($lang.realNameType.interface);
            }else{
                $("#type").val($lang.realNameType.manual);
            }
            $("#positiveIdcardPic").attr("src",ncGlobal.fileRoot + gridData.positiveIdcardPic);
            $("#oppositeIdcardPic").attr("src",ncGlobal.fileRoot + gridData.oppositeIdcardPic);
            _refreshImage();

        });

        //查看对话框初始化
        $("#lookModal").on("show.bs.modal", function (event) {

            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#lookNickName").val(gridData.user.nickName);
            $("#lookMobilePhone").val(gridData.user.mobile);
            $("#lookIdcard").val(gridData.idcard);
            $("#lookRealName").val(gridData.realName);
            var aStatus = gridData.authStatus;
            if( aStatus==1){
                $("#lookAuthStatus").val($lang.realNameStatus.WAIT);
            }else if(aStatus ==2 ){
                $("#lookAuthStatus").val($lang.realNameStatus.PASS);
            }else if (aStatus ==3 ){
                $("#lookAuthStatus").val($lang.realNameStatus.NOPASS);
            }
            var type = gridData.type;
            if(type == 1 ){
                $("#lookType").val($lang.realNameType.interface);
            }else{
                $("#lookType").val($lang.realNameType.manual);
            }
            $("#lookPositiveIdcardPic").attr("src",ncGlobal.fileRoot + gridData.positiveIdcardPic);
            $("#lookOppositeIdcardPic").attr("src",ncGlobal.fileRoot + gridData.oppositeIdcardPic);

            $("#lookRemark").val(gridData.remark);
            _refreshImage();

        });

        /**
         * 刷新addmodal 上的图片
         */
        function _refreshImage() {
            $(".viewImage").jqthumb({
                width: 180,
                height: 100,
                after: function (imgObj) {
                    imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
                }
            });
        }

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_real_name_or_lk_user.mobile_or_lk_idcard'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    /**
     * 删除入驻申请
     */
    function resetPwd(id) {
        var tpl = '您确定要重置密码吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "/user/pwdReset.json",
            data: {
                id: id
            },
            content: tpl
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        resetPwd: resetPwd
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});